package com.example.appslide

import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import android.widget.ArrayAdapter // 导入ArrayAdapter

class MainActivity : AppCompatActivity() {
    private val programExpert = ProgramExpert()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spinner = findViewById<Spinner>(R.id.spinner_feature)
        val queryButton = findViewById<Button>(R.id.button_query)
        val resultTextView = findViewById<TextView>(R.id.text_view_result)

        val addTextButton = findViewById<Button>(R.id.button_add_text)
        val dynamicContainer = findViewById<LinearLayout>(R.id.dynamic_text_container)

        // 为Spinner设置适配器
        val adapter = ArrayAdapter.createFromResource(
            this,
            R.array.feature_list,
            R.layout.spinner_dropdown_item
        )
        adapter.setDropDownViewResource(R.layout.spinner_dropdown_item)
        spinner.adapter = adapter

        queryButton.setOnClickListener {
            val selectedFeature = spinner.selectedItem.toString()
            resultTextView.text = programExpert.getLanguage(selectedFeature)
        }

        addTextButton.setOnClickListener {
            val count = dynamicContainer.childCount + 1
            val newTextView = TextView(this).apply {
                text = getString(R.string.new_text_view_format, count)
                textSize = 16f
                setTextColor(Color.parseColor("#8B4513")) // 使用KTX扩展函数String.toColorInt()
                setBackgroundColor(Color.parseColor("#FFE8B0")) // 使用KTX扩展函数String.toColorInt()
                setPadding(16, 16, 16, 16)
                layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                ).apply {
                    setMargins(16, 12, 16, 12)
                }
                background = ContextCompat.getDrawable(this@MainActivity, R.drawable.rounded_corner) // 使用AppCompatResources.getDrawable()相关的ContextCompat
            }
            dynamicContainer.addView(newTextView)
        }
    }
}